package com.eims.sls_app.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import bls.juejin.cn.R;
import com.eims.sls_app.dataaccess.utils.HttpUtils;
import com.eims.sls_app.protocol.MD5;
import com.eims.sls_app.protocol.RspBodyBaseBean;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.NetWork;
import com.eims.sls_app.utils.VerificationCodeUtils;
import com.eims.sls_app.view.MyToast;

import org.json.JSONException;
import org.json.JSONObject;

public class ResetPasswordActivity extends MyBaseActivity implements
		View.OnClickListener {
	private Context context = ResetPasswordActivity.this;
	private ImageButton btn_back;
	private EditText reset_password_edit, reset_password_comfir_edit,
			reset_password_checkma_edit;
	private Button reset_password_check_button, reset_password_ok;
	private VerificationCodeUtils verificationCodeUtils;
	private String pass, re_pass, check_ma;
	private MyAsynTask myAsynTask;
	private String moblie;
	private String opt, auth, info, time, imei, crc; // 格式化后的参数

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_reset_password);
		App.activityS.add(this);
		findView();
		init();
		setListener();
	}

	private void findView() {
		moblie = getIntent().getStringExtra("moblie");
		btn_back = (ImageButton) findViewById(R.id.btn_back);
		reset_password_edit = (EditText) findViewById(R.id.reset_password_edit);
		reset_password_comfir_edit = (EditText) findViewById(R.id.reset_password_comfir_edit);
		reset_password_checkma_edit = (EditText) findViewById(R.id.reset_password_checkma_edit);
		reset_password_check_button = (Button) findViewById(R.id.reset_password_check_button);
		reset_password_ok = (Button) findViewById(R.id.reset_password_ok);
	}

	private void init() {
		verificationCodeUtils = new VerificationCodeUtils(context,
				new VerificationCodeUtils.VerificationCodeListener() {
					@Override
					public void onCheckProgress(int status, int progress) {
						if (status == VerificationCodeUtils.FINISHED
								&& progress == -1) {
							reset_password_check_button.setText("重新获取验证码");
						} else if (status == VerificationCodeUtils.RUNNING) {
							reset_password_check_button.setText("(" + progress
									+ ")");
						}
					}

					@Override
					public void onCheckComplete(int params, String result,
							String msg) {
						switch (params) {
						case 0: // 请求短信验证码

							break;
						case 1: // 验证短信验证码
							if (result.equals(App.ERROR_SUCCESS + "")) {
								myAsynTask = new MyAsynTask();
								myAsynTask.execute();
							}
							break;
						}
					}
				});
	}

	private void setListener() {
		btn_back.setOnClickListener(this);
		reset_password_check_button.setOnClickListener(this);
		reset_password_ok.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu_reset_password, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();

		// noinspection SimplifiableIfStatement
		if (id == R.id.action_settings) {
			return true;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			finish();
			break;
		case R.id.reset_password_check_button:
			verificationCodeUtils.startCheck(0, moblie);
			break;
		case R.id.reset_password_ok:
			submit();
			break;
		}
	}

	private void submit() {
		pass = reset_password_edit.getText().toString();
		re_pass = reset_password_comfir_edit.getText().toString();
		check_ma = reset_password_checkma_edit.getText().toString();

		if (TextUtils.isEmpty(pass)) {
			MyToast.getToast(this, "请输入新的密码").show();
			return;
		}
		if (TextUtils.isEmpty(re_pass)) {
			MyToast.getToast(this, "请再次确认新的密码").show();
			return;
		}

		if (pass.trim().length() < 6) {
			MyToast.getToast(this, "输入密码不能至少6位").show();
			return;
		}
		if (!pass.equals(re_pass)) {
			MyToast.getToast(this, "两次输入的密码不一致").show();
			return;
		}
		if (TextUtils.isEmpty(check_ma)) {
			MyToast.getToast(this, "请输入短信验证码").show();
			return;
		}

		if (NetWork.isConnect(this)) {
			verificationCodeUtils.startCheck(1, moblie, check_ma);
		} else {
			MyToast.getToast(this, "请检查网络").show();
		}
	}

	// 重置密码请求
	private class MyAsynTask extends AsyncTask<Integer, Integer, String> {

		@Override
		protected String doInBackground(Integer... params) {
			String error = "-1";
			opt = "57";
			time = RspBodyBaseBean.getTime();
			imei = RspBodyBaseBean.getIMEI(context);
			String key = MD5.md5(App.MD5_key);

			pass = MD5.md5(pass + App.MD5_key);
			info = "{\"mobile\":\"" + moblie + "\",\"password\":\"" + pass
					+ "\"}";
			crc = RspBodyBaseBean.getCrc(time, imei, key, info, "-1");
			auth = RspBodyBaseBean.getAuth(crc, time, imei, "-1");

			String values[] = { opt, auth, info };
			String result = HttpUtils.doPost(App.names, values,
					App.path);

			Log.i("x", "重置密码info---------" + info);
			Log.i("x", "重置密码result----" + result);

			if ("-500".equals(result))
				return result;
			try {
				JSONObject ob = new JSONObject(result);
				error = ob.getString("error");
				if ("0".equals(error)) {
					return App.ERROR_SUCCESS + "";
				}
			} catch (JSONException e) {
				Log.w("TAG", "JSONException==>" + e.toString());
				error = "-1";
			}
			return error;
		}

		@Override
		protected void onPostExecute(String s) {
			super.onPostExecute(s);
			switch (Integer.valueOf(s)) {
			case 0:
				Intent intent = new Intent(context, LoginActivity.class);
				startActivity(intent);
				finish();
				break;
			case -1:
				MyToast.getToast(context, "密码重置失败").show();
				break;
			}
		}
	}
}
